<template>
  <div class="wrapper">
    <div style="margin: 200px 0 0 70%;  background-color: rgba(245,239,239,0.8); width: 350px; height: 385px; padding: 20px; border-radius: 10px">
      <div style="margin: 20px 0; text-align: center; font-size: 24px;color:mediumpurple"><b>欢迎注册</b></div>
        <div style="flex: 1; display: flex; align-items: center; justify-content: center">
          <el-form :model="user" style="width: 80%" :rules="rules" ref="registerRef">
            <el-form-item prop="username">
              <el-input prefix-icon="el-icon-user" size="medium" placeholder="请输入账号" v-model="user.username"></el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input prefix-icon="el-icon-lock" size="medium" show-password placeholder="请输入密码" v-model="user.password"></el-input>
            </el-form-item>
            <el-form-item prop="confirmPass">
              <el-input prefix-icon="el-icon-lock" size="medium" show-password placeholder="请确认密码" v-model="user.confirmPass"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="info" style="width: 100%;background-color: mediumpurple" @click="register">注 册</el-button>
            </el-form-item>
            <div style="display: flex">
              <div style="flex: 1">已经有账号了，请 <span style="color: mediumpurple; cursor: pointer" @click="$router.push('/login')">登录</span></div>
            </div>
          </el-form>
        </div>
      </div>
    </div>
</template>

<script>

export default {
  name: "Register",
  data() {
    // 验证码校验
    const validatePassword = (rule, confirmPass, callback) => {
      if (confirmPass === '') {
        callback(new Error('请确认密码'))
      } else if (confirmPass !== this.user.password) {
        callback(new Error('两次输入的密码不一致'))
      } else {
        callback()
      }
    }
    return {
      user: {
        username: '',
        password: '',
        confirmPass: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
        ],
        confirmPass: [
          { validator: validatePassword, trigger: 'blur' }
        ],
        role: [
          { required: true, message: '请选择角色', trigger: 'blur' },
        ],
      }
    }
  },
  created() {

  },
  methods: {
    register() {
      this.$refs['registerRef'].validate((valid) => {
        if (valid) {
          // 验证通过
          this.$request.post('/register', this.user).then(res => {
            if (res.code === '200') {
              this.$router.push('/login')
              this.$message.success('注册成功')
            } else {
              this.$message.error(res.msg)
            }
          })
        }
      })
    }
  }
}
</script>

<style scoped>
.wrapper {
  height: 100vh;
  background-image: url("@/assets/bg.jpeg");
  background-size: cover;
  overflow: hidden;
}
</style>